package furns.service.impl;

import com.sun.crypto.provider.PBEWithMD5AndDESCipher;
import furns.dao.FurnDAO;
import furns.dao.impl.FurnDAOImpl;
import furns.entity.Furn;
import furns.entity.Page;
import furns.service.FurnService;

import java.util.List;

public class FurnServiceImpl implements FurnService {
    private FurnDAO furnDAO = new FurnDAOImpl();
    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }

    @Override
    public boolean addFurn(Furn furn) {
       return furnDAO.addFurn(furn) == 1? true : false;
    }

    @Override
    public int delFurnById(int id) {
        return furnDAO.delFurnById(id);
    }

    @Override
    public Furn queryFurnById(int id) {
        return furnDAO.queryFurnById(id);
    }

    @Override
    public int updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn);
    }

    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        //先创建一个Page对象，然后根据实际情况填充属性
        Page<Furn> furnPage = new Page<>();
        //当前页
        furnPage.setPageNo(pageNo);
        //一页的有多少数据
        furnPage.setPageSize(pageSize);
        //总共有多少记录
        int totalRow = furnDAO.getTotalRow();
        furnPage.setTotalRow(totalRow);
        //总共有多少页
        int pageTotalCount = totalRow / pageSize;
        if (totalRow % pageSize > 0){
            pageTotalCount += 1;
        }
        furnPage.setPageTotalCount(pageTotalCount);

        //每页的数据
        //但是注意这里隐藏一个坑, 现在你看不到, 后面会暴露
        int begin = (pageNo -1) * pageSize;
        List<Furn> pageItem = furnDAO.getPageItem(begin, pageSize);
        furnPage.setItems(pageItem);
        return furnPage;
    }

    @Override
    public Page<Furn> getPageByName(int pageNo, int pageSize,String name) {
        //先创建一个Page对象，然后根据实际情况填充属性
        Page<Furn> furnPage = new Page<>();
        //当前页
        furnPage.setPageNo(pageNo);
        //一页的有多少数据
        furnPage.setPageSize(pageSize);
        //总共有多少记录
        int totalRow = furnDAO.getTotalRowByName(name);
        furnPage.setTotalRow(totalRow);
        //总共有多少页
        int pageTotalCount = totalRow / pageSize;
        if (totalRow % pageSize > 0){
            pageTotalCount += 1;
        }
        furnPage.setPageTotalCount(pageTotalCount);
        //每页的数据
        //但是注意这里隐藏一个坑, 现在你看不到, 后面会暴露
        int begin = (pageNo -1) * pageSize;
        List<Furn> pageItem = furnDAO.getPageItemByName(begin,pageSize,name);
        furnPage.setItems(pageItem);
        return furnPage;
    }
}
